//
//  QCSegmentedListContainerView.swift
//  SegmentedView
//
//  Created by lizhi on 2021/12/10.
//

import UIKit

protocol QCSegmentedListContainerViewDataSource: AnyObject {
  func searchClick(inputText: String)
  func numberOfLists(in listContainerView: QCSegmentedListContainerView) -> Int
    
    /// - Parameters:
    ///   - listContainerView: JXSegmentedListContainerView
    ///   - index: 目标index
    /// - Returns: 遵从JXSegmentedListContainerViewListDelegate协议的实例
    func listContainerView(_ listContainerView: QCSegmentedListContainerView, initListAt index: Int) -> QCSegmentedListContainerViewListDelegate
}

@objc
protocol QCSegmentedListContainerViewListDelegate :AnyObject {
    /// - Returns: 返回列表视图
    func listView() -> UIView
    @objc optional func listWillAppear()
    @objc optional func listDidAppear()
    @objc optional func listWillDisappear()
    @objc optional func listDidDisappear()
}


class QCSegmentedListContainerView: UIView {
    public   var dataSource: QCSegmentedListContainerViewDataSource?
    public lazy var scrollView = UIScrollView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setup()  {
        scrollView.frame = CGRect(x: 0, y: 0, width: 300, height: 400)
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        }
        
        self.addSubview(scrollView)
        scrollView.backgroundColor = UIColor.purple
        
        
//        UIView *aView = UIView(frame: scrollView.bounds)
//        aView.back
        let aView = UIView(frame: scrollView.bounds)
        scrollView.addSubview(aView)
        
        aView.backgroundColor = UIColor.red
        
        
        let bView = UIView(frame: CGRect(x: scrollView.frame.size.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height))
        scrollView.addSubview(bView)
        
        bView.backgroundColor = UIColor.orange
        
//        scrollView.contentSize = CGSize(width: scrollView.bounds.size.width * 2, height: scrollView.frame.size.height)
        scrollView.contentSize = CGSize(width: 600, height: 400)
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        scrollView.frame = bounds
        scrollView.contentSize =  CGSize(width: scrollView.bounds.size.width*2, height: scrollView.bounds.size.height)
        for (index,aView) in scrollView.subviews.enumerated() {
            aView.frame = CGRect(x: scrollView.frame.width * CGFloat(index), y: scrollView.frame.origin.y, width: scrollView.frame.width, height: scrollView.frame.height)
        }
        print("----- bounds = \(bounds)")
    }
}

extension QCSegmentedListContainerView {
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard scrollView.isTracking || scrollView.isDragging else {
            return
        }
        
        let percent = scrollView.contentOffset.x/scrollView.bounds.size.width
        let maxCount = Int(round(scrollView.contentSize.width/scrollView.bounds.size.width))
        var leftIndex = Int(floor(Double(percent)))
        leftIndex = max(0, min(maxCount - 1, leftIndex))
        let rightIndex = leftIndex + 1;
        if percent < 0 || rightIndex >= maxCount {
//            listDidAppearOrDisappear(scrollView: scrollView)
            return
        }
        let remainderRatio = percent - CGFloat(leftIndex)
//        if rightIndex == currentIndex {
//            //当前选中的在右边，用户正在从右边往左边滑动
//            if validListDict[leftIndex] == nil && remainderRatio < (1 - initListPercent) {
//                initListIfNeeded(at: leftIndex)
//            }else if validListDict[leftIndex] != nil {
//                if willAppearIndex == -1 {
//                    willAppearIndex = leftIndex;
//                    listWillAppear(at: willAppearIndex)
//                }
//            }
//
//            if willDisappearIndex == -1 {
//                willDisappearIndex = rightIndex
//                listWillDisappear(at: willDisappearIndex)
//            }
//        }else {
//            //当前选中的在左边，用户正在从左边往右边滑动
//            if validListDict[rightIndex] == nil && remainderRatio > initListPercent {
//                initListIfNeeded(at: rightIndex)
//            }else if validListDict[rightIndex] != nil {
//                if willAppearIndex == -1 {
//                    willAppearIndex = rightIndex
//                    listWillAppear(at: willAppearIndex)
//                }
//            }
//            if willDisappearIndex == -1 {
//                willDisappearIndex = leftIndex
//                listWillDisappear(at: willDisappearIndex)
//            }
//        }
//        listDidAppearOrDisappear(scrollView: scrollView)
    }
}



